package controllers

import (
	"log"
	"xmgl/models"

	//"log"
	"net/http"

	"github.com/astaxie/beego"
)

/*
运维服务，工单登记
*/
type WwfwGddjController struct {
	beego.Controller
}

//Api ,取得所有工单GET，新增修改工单POST，删除工单DELETE
type Api_gdController struct {
	beego.Controller
}

//Api ,运维服务，取得分页工单GET
type Api_fygdController struct {
	beego.Controller
}

func init() {

}

func (this *WwfwGddjController) Get() {
	var (
		menu models.Menu_gl
		user models.Sut_user
	)
	if this.GetSession("USER") == nil {
		//没有取到session值，页面跳转
		http.Redirect(this.Ctx.ResponseWriter, this.Ctx.Request, "/login", http.StatusFound)
	}
	tmp, ok := this.GetSession("USER").(models.Sut_user) //注意类型断言
	if ok {
		user = tmp
	}
	//log.Println("从session取用户名：", tmp.User_name, "ID：", tmp.User_id)
	//sess := this.StartSession()
	//sess.Set("USER", user)
	//log.Println("从session取用户名：", sess.Get("USER"))

	this.Data["WEBSITE"] = "运维管理/工单登记"
	this.Data["MENU_LEFT"] = menu.Get_left_menu()
	this.Data["MENU_RIGHT"] = menu.Get_right_menu()
	this.Data["USER_NAME"] = user.User_name

	this.Layout = "layout.html"
	this.TplName = "wwfwgddj.html"
	this.LayoutSections = make(map[string]string)
	this.LayoutSections["HtmlHeader"] = "html_header.html"
}

//返回所有工单或根据ID返回单个工单
func (this *Api_gdController) Get() {
	var (
		//err error
		gd_id int
		tmp   models.Sut_gd
		gds   []models.Sut_gd
	)
	gd_id, _ = this.GetInt(":id", -1) //取数字值
	//log.Println("this.Ctx.Input.Param(':id'):", gd_id)
	if gd_id == -1 {
		//提交的没有ID号这个字段，则返回所有工单
		gds = tmp.Get_gds(0)
	} else {
		//取到了ID号，则只返回这个ID号的工单
		gds = tmp.Get_gds(gd_id)
	}
	this.Data["json"] = gds
	this.ServeJSON()
}

//返回分页查询的工单/api/v1/fygd
//，使用bootstrapTable的服务器端分页,前端提交pageSize,pageNumber，需要返回的数据有总记录数和行数据
func (this *Api_fygdController) Get() {
	var (
		//err error
		t          models.Bootstrap_fy
		pageSize   int
		pageNumber int
		showAll    bool
		tmp        models.Sut_gd
	)
	//gd_id, _ = this.GetInt(":id", -1) //取数字值或this.Ctx.Input.Param(':id')
	pageSize, _ = this.GetInt("pageSize")
	pageNumber, _ = this.GetInt("pageNumber")
	showAll, _ = this.GetBool("showAll", false) //为真，则显示所有用户，为假，则显示当前用户的数据
	//log.Println("pageSize:", this.GetString("pageSize"), "，pageNumber:", this.GetString("pageNumber"), "，showAll:", showAll)
	if showAll {
		//为真
		t = tmp.Get_fy_gd(pageSize, pageNumber, 0)
	} else {
		//为假
		//取得用户ID号
		user := this.GetSession("USER").(models.Sut_user)
		//log.Println("当前用户ID为：", user.User_id, "，当前用户名：", user.User_name)
		t = tmp.Get_fy_gd(pageSize, pageNumber, user.User_id)
	}
	//提交的没有ID号这个字段，则返回所有工单
	this.Data["json"] = t
	this.ServeJSON()
}

//提交工单
func (this *Api_gdController) Post() {
	var (
		u models.Sut_user
		//err error
		gd models.Sut_gd
	)
	gd = models.Sut_gd{}
	gd.Riqi = this.GetString("riqi")
	gd.Jsmc = this.GetString("jsmc")
	gd.Jc, _ = this.GetInt("jc")
	gd.Gzxx = this.GetString("gzxx")
	gd.Clfs_id, _ = this.GetInt("clfs_id")
	gd.Clnr = this.GetString("clnr")
	gd.Jjsj = this.GetString("jjsj")
	gd.Gzlx_id, _ = this.GetInt("gzlx_id")
	gd.Gzsb_id, _ = this.GetInt("gzsb_id")
	u = this.GetSession("USER").(models.Sut_user) //通过SESSION取得用户
	gd.Jsr_id = u.User_id
	if err := this.ParseForm(&gd); err != nil {
		//handle error
		log.Println("func (this *Api_gdController) Post() 解析到工单结构体出错！")
	}
	//log.Println("收到提交的增加工单请求，gd:", gd)
	_ = gd.Insert() //保存工单到数据库
	//log.Println("func (this *Api_gdController) Post()：", err)
	this.Data["json"] = nil
	this.ServeJSON()
}
